package com.lin.views;

import com.lin.activity.AddFriendActivity;
import com.lin.activity.DecoderActivity;
import com.lin.utils.Utils;
import com.yunfeng.supercall.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

public class CustomPopupWindow extends PopupWindow {
	Activity context;
	public CustomPopupWindow(Activity context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		iniPopupWindow();
	}
	
	private void iniPopupWindow() {
		// TODO Auto-generated method stub

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.popupwindow, null);
		//		lv = (ListViewForGroupPopup) layout.findViewById(R.id.lv_pop_listview);
		//		SimpleAdapter adapter = new SimpleAdapter(this, l, R.layout.layout_for_listview, new String[]{"key"},new int[]{R.id.textview_in_listview});
		LinearLayout mLinearLayoutAddFriend = (LinearLayout) layout.findViewById(R.id.addfriend);
		LinearLayout mLinearLayoutScan = (LinearLayout) layout.findViewById(R.id.scan);
		LinearLayout mLinearLayoutCreateUser= (LinearLayout) layout.findViewById(R.id.create_user);
		
		mLinearLayoutAddFriend.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(context, AddFriendActivity.class);
				context.startActivity(intent);
				show();
			}
		});
		
		mLinearLayoutScan.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(context, DecoderActivity.class);
				context.startActivity(intent);
				show();
			}
		});
		
		mLinearLayoutCreateUser.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Utils.createUser(context);
				show();
			}
		});
		setContentView(layout);
		setFocusable(true);

		setWidth(450);
		setHeight(400);

		setBackgroundDrawable(context.getResources().getDrawable(R.drawable.popupwindow));
		setOutsideTouchable(true);
	}
	
	public void show() {
		if(isShowing()){
			dismiss();
		}else{
			View parent = context.getWindow().getDecorView();//��ô��壬��popupwindow�ĸ��ؼ�
			DisplayMetrics metric = new DisplayMetrics();
			context.getWindowManager().getDefaultDisplay().getMetrics(metric);
			int width = metric.widthPixels;     // 屏幕宽度（像素）
			showAtLocation(parent, Gravity.NO_GRAVITY, width, (int) context.getResources().getDimension(R.dimen.top_bar) + 60);//��ʾ����Ļ�м�
		}		
	}
	
}